package cn.sfrog.project.biz.admin.controller;

import cn.sfrog.common.annotation.Log;
import cn.sfrog.common.annotation.RepeatSubmit;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.enums.BusinessType;
import cn.sfrog.common.utils.poi.ExcelUtil;
import cn.sfrog.project.biz.admin.domain.vo.BizUserAccountFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserAccountQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizUserAccountVO;
import cn.sfrog.project.biz.admin.service.IBizUserAccountService;
import cn.sfrog.project.biz.admin.service.IBizUserTradeRecordService;
import cn.sfrog.project.biz.web.vo.account.AppUserTradeRecordQueryVO;
import cn.sfrog.project.biz.web.vo.account.AppUserTradeRecordResultVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@Api(tags = "管理后台-用户账户相关")
@RestController
@RequestMapping("/biz/account")
public class BizUserAccountController extends BaseController {
    @Resource
    private IBizUserAccountService bizUserAccountService;
    @Resource
    private IBizUserTradeRecordService bizUserTradeRecordService;

    /**
     * 查询用户账户列表
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('biz:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizUserAccountQueryVO queryVO) {
        startPage();
        List<BizUserAccountVO> list = bizUserAccountService.selectBizUserAccountList(queryVO);
        return getDataTable(list);
    }

    /**
     * 导出用户账户列表
     */
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('biz:account:export')")
    @Log(title = "用户账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizUserAccountQueryVO query) {
        List<BizUserAccountVO> list = bizUserAccountService.selectBizUserAccountList(query);
        ExcelUtil<BizUserAccountVO> util = new ExcelUtil<BizUserAccountVO>(BizUserAccountVO.class);
        util.exportExcel(response, list, "用户账户数据");
    }

    /**
     * 获取用户账户详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('biz:account:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizUserAccountService.selectBizUserAccountById(id));
    }

    /**
     * 新增用户账户
     */
    @ApiOperation("新增")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:account:add')")
    @Log(title = "用户账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizUserAccountFormVO form) {
        return toAjax(bizUserAccountService.insertBizUserAccount(form));
    }

    /**
     * 修改用户账户
     */
    @ApiOperation("修改")
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('biz:account:edit')")
    @Log(title = "用户账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizUserAccountFormVO form) {
        return toAjax(bizUserAccountService.updateBizUserAccount(form));
    }

    /**
     * 删除用户账户
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('biz:account:remove')")
    @Log(title = "用户账户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizUserAccountService.deleteBizUserAccountByIds(ids));
    }

    /**
     * 更改状态用户账户
     */
    @ApiOperation("更改状态")
    @PreAuthorize("@ss.hasPermi('biz:account:changeStatus')")
    @Log(title = "用户账户", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody @Valid BizStatusChangeVO vo) {
        return AjaxResult.success(bizUserAccountService.changeStatus(vo));
    }

    @ApiOperation("获取账户总额")
    @GetMapping("/getAllUserAccountAmount")
    public AjaxResult getAllUserAccountAmount(){
        return AjaxResult.success(bizUserAccountService.selectAllUserAccountAmount());
    }

    @ApiOperation("交易明细")
    @PreAuthorize("@ss.hasPermi('biz:account:getTradeRecordList')")
    @GetMapping("/getTradeRecordList")
    public TableDataInfo getTradeRecordList(AppUserTradeRecordQueryVO queryVO){
        startPage();
        List<AppUserTradeRecordResultVO> tradeRecordList = bizUserTradeRecordService.selectAppUserTradeRecordList(queryVO);
        return getDataTable(tradeRecordList);
    }
}
